/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package instances.SeedOfDestruction;

import java.util.Calendar;
import java.util.Map;

import javolution.util.FastMap;
import net.l2emuproject.gameserver.manager.gracia.SeedOfDestructionManager;
import net.l2emuproject.gameserver.manager.instances.InstanceManager;
import net.l2emuproject.gameserver.manager.instances.PartyInstance;
import net.l2emuproject.gameserver.manager.instances.InstanceManager.InstanceWorld;
import net.l2emuproject.gameserver.network.SystemMessageId;
import net.l2emuproject.gameserver.network.serverpackets.ExShowScreenMessage;
import net.l2emuproject.gameserver.network.serverpackets.SystemMessage;
import net.l2emuproject.gameserver.services.party.L2CommandChannel;
import net.l2emuproject.gameserver.services.party.L2Party;
import net.l2emuproject.gameserver.services.quest.QuestState;
import net.l2emuproject.gameserver.skills.L2Skill;
import net.l2emuproject.gameserver.system.util.Util;
import net.l2emuproject.gameserver.world.L2World;
import net.l2emuproject.gameserver.world.object.L2Npc;
import net.l2emuproject.gameserver.world.object.L2Player;
import net.l2emuproject.gameserver.world.object.instance.L2DoorInstance;

import org.apache.commons.lang.ArrayUtils;

/**
 * @author Gigiikun
 * 	Remade for L2EmuProject by lord_rex & lewzer
 * 
 * TODO:
 * - Traps not done
 * - no random mob spawns after mob kill
 */
public final class SeedOfDestruction extends PartyInstance
{
	private final class SODWorld extends InstanceWorld
	{
		private Map<L2Npc, Boolean>	npcList					= new FastMap<L2Npc, Boolean>();
		private int					killedDevice			= 0;
		@SuppressWarnings("unused")
		private int					deviceSpawnedMobCount	= 0;

		public SODWorld()
		{
			InstanceManager.getInstance().super();
		}
	}

	private static final String		QN							= "SeedOfDestruction";

	private static final String		TEMPLATE					= "SeedOfDestruction.xml";

	private static final int		INSTANCE_ID					= 110;															// this is the client number

	private static final int		MIN_PLAYERS					= 36;
	private static final int		MAX_PLAYERS					= 45;															// prevent too much mob spawn

	// Items

	// NPCs,mobs
	private static final int		ALENOS						= 32526;
	private static final int		TELEPORT					= 32601;
	private static final int		OBELISK						= 18776;
	private static final int		POWERFUL_DEVICE				= 18777;
	private static final int		THRONE_POWERFUL_DEVICE		= 18778;
	private static final int		TIAT						= 29163;
	private static final int[]		TIAT_VIDEO_NPC				=
																{ 29167, -251022, 211660, -11957 };							// this is a temp id
	private static final int[]		MOB_IDS						=
																{
			22536,
			22537,
			22538,
			22539,
			22540,
			22541,
			22542,
			22543,
			22544,
			22547,
			22550,
			22551,
			22552,
			22596,
			29162												};

	// Doors/Walls/Zones
	private static final int[]		ATTACKABLE_DOORS			=
																{
			12240005,
			12240006,
			12240007,
			12240008,
			12240009,
			12240010,
			12240013,
			12240014,
			12240015,
			12240016,
			12240017,
			12240018,
			12240021,
			12240022,
			12240023,
			12240024,
			12240025,
			12240026,
			12240028,
			12240029,
			12240030											};
	private static final int[]		ENTRANCE_ROOM_DOORS			=
																{ 12240001, 12240002 };
	private static final int[]		SQUARE_DOORS				=
																{ 12240003, 12240004, 12240011, 12240012, 12240019, 12240020 };
	private static final int		SCOUTPASS_DOOR				= 12240027;
	private static final int		FORTRESS_DOOR				= 12240030;
	private static final int		THRONE_DOOR					= 12240031;

	// spawns
	private static final int[][]	ENTRANCE_UPPER_SPAWNS		=
																{
																{ 22549, -239298, 220214, -9792, 32637 },
																{ 22549, -239294, 220064, -9760, 30975 },
																{ 22549, -239290, 219890, -9760, 29173 },
																{ 22549, -239294, 219728, -9800, 27648 },
																{ 22549, -239294, 220137, -9768, 31778 },
																{ 22549, -239294, 219806, -9776, 28351 },
																{ 22593, -239294, 219984, -9752, 32767 } };
	private static final int[][]	ENTRANCE_GROUND_SPAWNS_1	=
																{
																{ 22541, -240640, 220424, -10144, 0 },
																{ 22541, -240010, 219423, -10144, 0 },
																{ 22541, -240073, 219696, -10144, 0 },
																{ 22541, -239629, 220296, -10144, 54894 },
																{ 22541, -240502, 219707, -10144, 32238 },
																{ 22550, -240577, 220242, -10144, 0 },
																{ 22550, -239947, 219514, -10144, 0 },
																{ 22550, -240262, 220697, -10144, 0 },
																{ 22550, -240325, 220151, -10144, 0 },
																{ 22550, -239846, 220660, -10139, 31613 },
																{ 22551, -240199, 220606, -10144, 0 },
																{ 22551, -240577, 219969, -10136, 0 },
																{ 22551, -240514, 220424, -10144, 0 },
																{ 22551, -240010, 220606, -10144, 0 },
																{ 22551, -240307, 219543, -10144, 64355 },
																{ 22552, -240640, 219332, -10144, 0 },
																{ 22552, -239769, 219463, -10128, 19399 },
																{ 22552, -240073, 220333, -10144, 0 },
																{ 22552, -240262, 220151, -10144, 0 },
																{ 22552, -239886, 219923, -10144, 44774 },
																{ 22596, -239504, 219984, -10136, 32767 } };
	private static final int[][]	ENTRANCE_GROUND_SPAWNS_2	=
																{
																{ 22541, -238845, 219557, -10144, 22378 },
																{ 22541, -238971, 220184, -10144, 40988 },
																{ 22541, -238498, 219515, -10144, 47694 },
																{ 22544, -238728, 220066, -10144, 35011 },
																{ 22544, -238524, 220521, -10144, 0 },
																{ 22544, -238675, 220632, -10135, 16075 },
																{ 22597, -239151, 220044, -10128, 32767 },
																{ 22597, -239152, 219988, -10128, 32767 },
																{ 22597, -239152, 219934, -10128, 32767 },
																{ 32601, -238310, 219984, -10104, 0 } };
	private static final int[][]	SQUARE_SPAWNS_STATIC		=
																{
																{ 18776, -245817, 217075, -12208, 0 },
																{ 32601, -245820, 220921, -12091, 0 } };
	private static final int[][]	SQUARE_SPAWNS_MAIN			=
																{
																{ 22541, -245714, 216761, -12208, 11487 },
																{ 22541, -245563, 217294, -12208, 33720 },
																{ 22544, -246104, 217132, -12208, 62589 },
																{ 22543, -245857, 217391, -12208, 57177 },
																{ 22544, -245408, 216484, -12224, 18467 },
																{ 22543, -247062, 216734, -12227, 23550 },
																{ 22541, -244854, 218073, -12224, 0 },
																{ 22543, -245052, 217890, -12224, 0 },
																{ 22544, -245459, 218451, -12224, 0 },
																{ 22543, -247057, 217496, -12227, 33700 },
																{ 22543, -246631, 217874, -12227, 34490 },
																{ 22541, -246571, 218141, -12227, 10158 },
																{ 22547, -246398, 217639, -12227, 33120 },
																{ 22542, -245427, 217725, -12227, 36580 },
																{ 22544, -244803, 217927, -12227, 5277 },
																{ 22547, -245291, 217577, -12227, 51169 },
																{ 22540, -245607, 217891, -12227, 20383 },
																{ 22544, -245685, 218236, -12227, 6661 },
																{ 22543, -245808, 218103, -12227, 41367 },
																{ 22541, -245931, 217830, -12227, 44736 },
																{ 22543, -245579, 217581, -12227, 57648 },
																{ 22540, -245447, 218056, -12227, 23464 },
																{ 22544, -245521, 216836, -12210, 52857 },
																{ 22543, -245248, 216647, -12227, 7152 },
																{ 22543, -245985, 216854, -12209, 4136 },
																{ 22542, -246093, 217649, -12227, 59266 },
																{ 22544, -245460, 217107, -12209, 41136 },
																{ 22541, -245721, 217415, -12209, 51094 },
																{ 22544, -246027, 217339, -12209, 44597 },
																{ 22540, -245981, 218357, -12227, 36676 },
																{ 22541, -246027, 218150, -12227, 46567 },
																{ 22544, -246158, 217947, -12227, 42906 },
																{ 22540, -246255, 218286, -12227, 13265 },
																{ 22541, -246642, 217365, -12227, 28136 },
																{ 22543, -246202, 217349, -12224, 39524 },
																{ 22543, -246508, 216967, -12227, 47366 },
																{ 22547, -246915, 216896, -12227, 43613 },
																{ 22540, -245805, 216469, -12227, 6356 },
																{ 22542, -244730, 217074, -12227, 1751 },
																{ 22541, -244853, 217477, -12227, 21465 },
																{ 22540, -245141, 217286, -12227, 39311 },
																{ 22544, -244639, 217274, -12229, 63192 },
																{ 22541, -244880, 216618, -12227, 65082 },
																{ 22544, -244755, 216345, -12229, 4993 },
																{ 22547, -245129, 216296, -12227, 32123 },
																{ 22542, -245407, 216273, -12227, 29439 },
																{ 22541, -245389, 215889, -12227, 40959 },
																{ 22541, -246288, 216516, -12227, 56087 },
																{ 22544, -246252, 216765, -12227, 14886 },
																{ 22542, -246067, 216269, -12229, 52690 },
																{ 22544, -246301, 216092, -12227, 39522 },
																{ 22541, -246056, 215948, -12227, 59993 },
																{ 22540, -245786, 215821, -12227, 60950 },
																{ 22547, -245841, 216144, -12229, 7010 },
																{ 22543, -246883, 217291, -12227, 35363 },
																{ 22543, -246642, 216317, -12227, 35363 },
																{ 22544, -246935, 216456, -12227, 27131 } };
	private static final int[][]	SQUARE_SPAWNS_HALF			=
																{
																{ 22541, -245714, 216761, -12208, 11487 },
																{ 22543, -245857, 217391, -12208, 57177 },
																{ 22544, -245408, 216484, -12224, 18467 },
																{ 22543, -245052, 217890, -12224, 0 },
																{ 22544, -245459, 218451, -12224, 0 },
																{ 22541, -246571, 218141, -12227, 10158 },
																{ 22547, -246398, 217639, -12227, 33120 },
																{ 22547, -245291, 217577, -12227, 51169 },
																{ 22540, -245607, 217891, -12227, 20383 },
																{ 22541, -245931, 217830, -12227, 44736 },
																{ 22543, -245579, 217581, -12227, 57648 },
																{ 22543, -245248, 216647, -12227, 7152 },
																{ 22543, -245985, 216854, -12209, 4136 },
																{ 22541, -245721, 217415, -12209, 51094 },
																{ 22544, -246027, 217339, -12209, 44597 },
																{ 22544, -246158, 217947, -12227, 42906 },
																{ 22540, -246255, 218286, -12227, 13265 },
																{ 22543, -246508, 216967, -12227, 47366 },
																{ 22547, -246915, 216896, -12227, 43613 },
																{ 22541, -244853, 217477, -12227, 21465 },
																{ 22540, -245141, 217286, -12227, 39311 },
																{ 22544, -244755, 216345, -12229, 4993 },
																{ 22547, -245129, 216296, -12227, 32123 },
																{ 22541, -246288, 216516, -12227, 56087 },
																{ 22544, -246252, 216765, -12227, 14886 },
																{ 22541, -246056, 215948, -12227, 59993 },
																{ 22540, -245786, 215821, -12227, 60950 },
																{ 22543, -246642, 216317, -12227, 35363 },
																{ 22544, -246935, 216456, -12227, 27131 } };
	private static final int[][]	CORRIDOR_SPAWNS_UPPER		=
																{
																{ 22593, -245888, 211792, -12000, 16384 },
																{ 22593, -246029, 211797, -12016, 16384 },
																{ 22593, -246119, 211796, -12040, 16384 },
																{ 22593, -245754, 211796, -12016, 16384 },
																{ 22593, -245652, 211793, -12032, 16384 },
																{ 22547, -245892, 208165, -12128, 16384 },
																{ 22593, -246032, 208176, -12144, 16384 },
																{ 22593, -246128, 208176, -12168, 16384 },
																{ 22593, -245728, 208176, -12144, 16384 },
																{ 22593, -245632, 208160, -12176, 16384 },
																{ 22549, -242559, 213163, -12168, 16384 },
																{ 22549, -242912, 213166, -12136, 16384 },
																{ 22549, -242676, 213164, -12136, 16384 },
																{ 22549, -243003, 213166, -12152, 16384 },
																{ 22593, -242800, 213152, -12128, 16384 },
																{ 22549, -241168, 218256, -12016, 32767 },
																{ 22549, -241168, 218176, -12008, 32767 },
																{ 22593, -241173, 218104, -12000, 32767 },
																{ 22549, -241168, 218336, -12040, 32767 },
																{ 22549, -241168, 218032, -12008, 32767 },
																{ 22549, -241168, 217936, -12016, 32767 },
																{ 22549, -241168, 217840, -12048, 32767 },
																{ 22549, -238634, 215891, -12264, 16384 },
																{ 22593, -238736, 215888, -12256, 16384 },
																{ 22549, -238545, 215891, -12272, 16384 },
																{ 22549, -238853, 215888, -12264, 16384 },
																{ 22549, -238466, 215890, -12304, 16384 },
																{ 22549, -238939, 215886, -12280, 16384 },
																{ 22549, -242026, 209558, -12320, 16384 },
																{ 22549, -242103, 209561, -12320, 16384 },
																{ 22549, -242180, 209563, -12320, 16384 },
																{ 22549, -242270, 209565, -12320, 16384 },
																{ 22549, -242349, 209565, -12320, 16384 },
																{ 22549, -242494, 209567, -12312, 16384 },
																{ 22549, -242422, 209568, -12312, 16384 },
																{ 22549, -242661, 209569, -12272, 16384 },
																{ 22549, -242560, 209568, -12296, 16384 },
																{ 22549, -243616, 209568, -12320, 16384 },
																{ 22549, -243035, 209559, -12296, 16384 },
																{ 22549, -242920, 209561, -12264, 16384 },
																{ 22593, -242800, 209557, -12256, 16384 },
																{ 22549, -243537, 209567, -12320, 16384 },
																{ 22549, -243375, 209564, -12320, 16384 },
																{ 22549, -243448, 209565, -12320, 16384 },
																{ 22549, -243310, 209564, -12320, 16384 },
																{ 22549, -243239, 209563, -12320, 16384 },
																{ 22549, -243158, 209561, -12312, 16384 },
																{ 22549, -239033, 215892, -12316, 17195 },
																{ 22593, -242969, 206669, -12416, 32764 },
																{ 22593, -242969, 206541, -12392, 32764 },
																{ 22593, -242969, 206349, -12392, 32764 },
																{ 22593, -242969, 206237, -12416, 32764 },
																{ 22593, -242969, 206445, -12384, 32764 },
																{ 22549, -239881, 208591, -12576, 32764 },
																{ 22549, -239881, 208447, -12576, 32764 },
																{ 22549, -239881, 208511, -12576, 32764 },
																{ 22549, -239897, 208351, -12576, 32764 },
																{ 22549, -239897, 208271, -12576, 32764 },
																{ 22549, -239897, 208207, -12568, 32764 },
																{ 22549, -239897, 208143, -12568, 32764 },
																{ 22549, -239897, 207135, -12576, 32764 },
																{ 22549, -239897, 207199, -12576, 32764 },
																{ 22549, -239897, 207263, -12576, 32764 },
																{ 22549, -239897, 207327, -12576, 32764 },
																{ 22549, -239897, 207391, -12576, 32764 },
																{ 22549, -239897, 207455, -12568, 32764 },
																{ 22549, -239897, 207519, -12568, 32764 },
																{ 22549, -238912, 212272, -12400, 16384 },
																{ 22593, -238738, 212263, -12384, 16384 },
																{ 22549, -238464, 212256, -12432, 16384 },
																{ 22549, -238960, 212272, -12416, 16384 },
																{ 22549, -238848, 212272, -12392, 16384 },
																{ 22549, -238640, 212272, -12392, 16384 },
																{ 22549, -238560, 212272, -12400, 16384 },
																{ 22547, -239792, 207824, -12512, 32764 },
																{ 22549, -239840, 207680, -12528, 32764 },
																{ 22549, -239840, 207776, -12512, 32764 },
																{ 22549, -239840, 207888, -12520, 32764 },
																{ 22549, -239840, 207984, -12528, 32764 },
																{ 22549, -239893, 207943, -12528, 32764 },
																{ 22549, -239893, 208036, -12544, 32764 },
																{ 22549, -239890, 207623, -12536, 32764 },
																{ 22549, -239890, 207714, -12520, 32764 },
																{ 22593, -239885, 207822, -12512, 32764 } };
	private static final int[][]	CORRIDOR_SPAWNS_GROUND		=
																{
																{ 22597, -245879, 211967, -12384, 16384 },
																{ 22541, -245436, 211460, -12384, 0 },
																{ 18720, -245888, 213056, -12376, 0 },
																{ 18737, -245696, 212608, -12384, 0 },
																{ 18721, -245920, 212256, -12384, 0 },
																{ 22541, -245997, 208982, -12512, 0 },
																{ 22541, -245733, 209162, -12512, 0 },
																{ 18739, -245888, 211296, -12384, 0 },
																{ 18722, -245888, 211008, -12384, 0 },
																{ 22540, -246513, 207291, -12512, 0 },
																{ 22541, -245909, 208682, -12512, 0 },
																{ 22597, -245882, 208369, -12512, 16384 },
																{ 22540, -245445, 207291, -12512, 0 },
																{ 18740, -245888, 209984, -12464, 0 },
																{ 18729, -245664, 209376, -12512, 0 },
																{ 18723, -246048, 209376, -12512, 0 },
																{ 18724, -246080, 208960, -12512, 0 },
																{ 18730, -246080, 208608, -12512, 0 },
																{ 22546, -243926, 205814, -12768, 0 },
																{ 18742, -245632, 208608, -12512, 0 },
																{ 18741, -245632, 208960, -12512, 0 },
																{ 18731, -246048, 207744, -12512, 0 },
																{ 18744, -246048, 207424, -12512, 0 },
																{ 18725, -245664, 207424, -12512, 0 },
																{ 18743, -245664, 207776, -12512, 0 },
																{ 18745, -245888, 206816, -12512, 0 },
																{ 18777, -241936, 206432, -12744, 0 },
																{ 22596, -242770, 206496, -12760, 32767 },
																{ 22596, -242770, 206384, -12760, 32767 },
																{ 22596, -242770, 206439, -12760, 32767 },
																{ 22544, -242620, 205989, -12768, 0 },
																{ 22541, -243611, 206269, -12768, 0 },
																{ 22541, -243233, 206633, -12768, 0 },
																{ 22597, -243177, 206476, -12768, 16384 },
																{ 22544, -242416, 207081, -12760, 0 },
																{ 22544, -242586, 206990, -12768, 0 },
																{ 18732, -243872, 206464, -12768, 0 },
																{ 18771, -243808, 206112, -12768, 0 },
																{ 22541, -243978, 206204, -12768, 0 },
																{ 22544, -243849, 206017, -12768, 0 },
																{ 18749, -243520, 206464, -12768, 0 },
																{ 18748, -243520, 206112, -12768, 0 },
																{ 22546, -240767, 208544, -12896, 0 },
																{ 18753, -242528, 206336, -12768, 0 },
																{ 18754, -242144, 206336, -12760, 0 },
																{ 18755, -242496, 206592, -12768, 0 },
																{ 18756, -242176, 206656, -12768, 0 },
																{ 18758, -242176, 206976, -12768, 0 },
																{ 22541, -245316, 211237, -12380, 8721 },
																{ 22541, -246223, 211076, -12389, 28836 },
																{ 22540, -245284, 207708, -12518, 1253 },
																{ 22540, -245836, 207628, -12518, 34643 },
																{ 22544, -243850, 206761, -12774, 15146 },
																{ 22546, -244017, 206672, -12774, 37875 },
																{ 22541, -243715, 206647, -12774, 64674 },
																{ 22544, -242237, 206167, -12774, 50344 },
																{ 22544, -242190, 206866, -12774, 6294 },
																{ 22548, -242723, 213682, -12512, 0 },
																{ 22548, -242987, 213922, -12512, 0 },
																{ 22548, -242195, 214102, -12512, 0 },
																{ 22541, -241550, 218362, -12384, 0 },
																{ 22596, -240992, 218032, -12368, 32767 },
																{ 22596, -240992, 218080, -12368, 32767 },
																{ 22596, -240992, 218176, -12368, 32767 },
																{ 22596, -240992, 218128, -12368, 32767 },
																{ 22541, -241994, 218628, -12384, 44063 },
																{ 22541, -242296, 218109, -12384, 38227 },
																{ 22597, -238784, 215696, -12624, 16384 },
																{ 22597, -238696, 215696, -12624, 16384 },
																{ 22596, -238736, 215696, -12624, 16384 },
																{ 22541, -238634, 215102, -12640, 0 },
																{ 22548, -238901, 215168, -12640, 0 },
																{ 22541, -238812, 215564, -12640, 0 },
																{ 22548, -239435, 215333, -12640, 0 },
																{ 22546, -238492, 213146, -12768, 0 },
																{ 22546, -238844, 213326, -12768, 0 },
																{ 22596, -238736, 212400, -12760, 16384 },
																{ 22546, -238932, 212606, -12768, 15087 },
																{ 22546, -238668, 212606, -12768, 0 },
																{ 22597, -238784, 212400, -12752, 16384 },
																{ 22597, -238688, 212400, -12752, 16384 },
																{ 22596, -238688, 212096, -12752, 16384 },
																{ 22596, -238784, 212095, -12752, 16384 },
																{ 22541, -238733, 212038, -12760, 16311 },
																{ 18777, -238736, 211248, -12744, 0 },
																{ 22596, -238736, 212096, -12752, 16384 },
																{ 22541, -238332, 211956, -12768, 30167 },
																{ 22546, -238736, 211527, -12768, 0 },
																{ 22543, -243503, 212324, -12512, 0 },
																{ 22540, -242613, 212489, -12512, 0 },
																{ 22543, -243147, 212852, -12504, 0 },
																{ 22540, -242524, 212786, -12512, 0 },
																{ 22540, -242346, 212588, -12512, 0 },
																{ 22548, -242282, 209234, -12640, 0 },
																{ 22546, -243627, 209944, -12640, 0 },
																{ 22548, -242638, 208640, -12632, 0 },
																{ 22548, -242104, 208838, -12640, 0 },
																{ 22596, -239696, 207776, -12888, 32767 },
																{ 22596, -239696, 207872, -12880, 32767 },
																{ 22596, -239696, 207824, -12888, 32767 },
																{ 22546, -240767, 207452, -12896, 0 },
																{ 22596, -240064, 207888, -12888, 32767 },
																{ 22596, -240016, 207872, -12880, 32767 },
																{ 22596, -240064, 207760, -12888, 32767 },
																{ 22596, -240016, 207776, -12880, 32767 },
																{ 22596, -240048, 207824, -12888, 32767 },
																{ 22548, -239100, 208369, -12896, 0 },
																{ 22548, -239495, 208098, -12896, 40057 },
																{ 22548, -239333, 208181, -12896, 41061 },
																{ 18777, -238848, 207824, -12864, 0 },
																{ 22539, -242826, 210708, -12645, 30758 },
																{ 22548, -243039, 210619, -12645, 2023 },
																{ 22548, -242570, 210599, -12645, 59674 },
																{ 22546, -242791, 210481, -12645, 24959 },
																{ 22548, -242925, 208656, -12631, 21437 },
																{ 22541, -243013, 209077, -12645, 33289 },
																{ 22547, -242759, 209004, -12645, 60509 },
																{ 22548, -243293, 208875, -12637, 29114 },
																{ 22541, -242335, 208954, -12639, 21264 },
																{ 22546, -240636, 208019, -12901, 26402 },
																{ 22548, -239507, 207680, -12901, 59320 },
																{ 22548, -239134, 207369, -12901, 30709 },
																{ 22548, -239376, 207273, -12896, 31526 },
																{ 22548, -239263, 207801, -12901, 26788 },
																{ 22539, -239070, 216742, -12644, 37062 },
																{ 22539, -238494, 216907, -12644, 1821 },
																{ 22546, -238485, 216645, -12644, 49855 },
																{ 22546, -238364, 216961, -12644, 16658 },
																{ 22546, -238901, 217073, -12644, 12726 },
																{ 22548, -238425, 215265, -12647, 27478 },
																{ 22539, -238495, 213307, -12773, 22966 },
																{ 22539, -238720, 213414, -12773, 42528 },
																{ 22546, -239014, 213051, -12773, 2058 },
																{ 22546, -239086, 211737, -12773, 12789 },
																{ 22541, -238688, 211910, -12773, 63943 },
																{ 22546, -238472, 211813, -12773, 21462 },
																{ 22541, -242276, 217966, -12389, 34120 },
																{ 22540, -242085, 218093, -12389, 36123 } };
	private static final int[][]	SCOUTPASS_SPAWNS_UPPER		=
																{
																{ 22593, -249696, 220496, -12128, 0 },
																{ 22549, -249696, 220416, -12120, 0 },
																{ 22549, -249696, 220576, -12128, 0 },
																{ 22593, -249696, 220640, -12128, 0 },
																{ 22549, -249696, 220704, -12128, 0 },
																{ 22593, -249696, 220768, -12128, 0 },
																{ 22549, -249712, 219984, -12072, 0 },
																{ 22549, -249696, 220848, -12128, 0 },
																{ 22549, -249712, 219888, -12080, 0 },
																{ 22549, -249712, 220272, -12088, 0 },
																{ 22549, -249712, 220176, -12072, 0 },
																{ 22593, -249712, 220080, -12064, 0 },
																{ 22549, -249714, 219498, -12128, 0 },
																{ 22593, -249721, 219421, -12128, 0 },
																{ 22549, -249732, 219349, -12128, 0 },
																{ 22593, -249730, 219266, -12128, 0 },
																{ 22549, -249717, 219565, -12128, 0 },
																{ 22593, -249727, 219649, -12128, 0 },
																{ 22549, -249728, 219728, -12120, 0 } };
	private static final int[][]	SCOUTPASS_SPAWNS_GROUND		=
																{
																{ 22538, -248393, 219615, -12448, 0 },
																{ 22539, -250349, 219423, -12440, 0 },
																{ 22539, -250315, 220060, -12448, 0 },
																{ 22539, -250553, 220879, -12448, 0 },
																{ 22538, -250451, 220879, -12448, 0 },
																{ 22538, -250553, 220242, -12448, 0 },
																{ 22540, -250485, 220333, -12448, 0 },
																{ 22538, -250485, 219423, -12448, 0 },
																{ 22540, -250553, 219969, -12440, 0 },
																{ 22540, -250417, 219423, -12448, 0 },
																{ 22540, -248671, 220289, -12453, 53437 },
																{ 22546, -248743, 219598, -12453, 61908 },
																{ 22546, -248778, 219835, -12453, 10079 },
																{ 22539, -248779, 220177, -12453, 757 } };
	private static final int[][]	PREFORT_SPAWNS				=
																{
																{ 22540, -249856, 216729, -12248, 0 },
																{ 22541, -250718, 217437, -12288, 0 },
																{ 22541, -250184, 217107, -12288, 0 },
																{ 22544, -251074, 217317, -12288, 0 },
																{ 22540, -250540, 217497, -12288, 0 },
																{ 22546, -250184, 217287, -12288, 0 },
																{ 22546, -250362, 217347, -12288, 0 },
																{ 22547, -252676, 217137, -12288, 0 },
																{ 22544, -253032, 217137, -12288, 0 },
																{ 22546, -252320, 217617, -12288, 0 },
																{ 22546, -253032, 217437, -12288, 0 },
																{ 22546, -252142, 217437, -12288, 0 },
																{ 22536, -252675, 215945, -12208, 0 },
																{ 22537, -252707, 215897, -12208, 0 },
																{ 22539, -252835, 215945, -12216, 0 },
																{ 22542, -252835, 216009, -12224, 0 },
																{ 22546, -252786, 216450, -12248, 0 },
																{ 22539, -253056, 216510, -12248, 0 },
																{ 22541, -252786, 216675, -12248, 0 },
																{ 22541, -252726, 216525, -12248, 0 },
																{ 22541, -253086, 216555, -12248, 0 },
																{ 22540, -252606, 216645, -12248, 0 },
																{ 22540, -252606, 216540, -12248, 0 },
																{ 22537, -250206, 215762, -12208, 0 },
																{ 22536, -250078, 215906, -12208, 0 },
																{ 22539, -250270, 215874, -12200, 0 },
																{ 22542, -249886, 215794, -12208, 0 },
																{ 22546, -251536, 215728, -12208, 16384 },
																{ 22546, -251345, 215712, -12208, 16384 },
																{ 22546, -251265, 215856, -12208, 16384 },
																{ 22546, -251584, 215856, -12208, 16384 },
																{ 22546, -251425, 215856, -12208, 16384 },
																{ 22540, -249891, 216649, -12248, 0 },
																{ 22547, -250031, 216585, -12248, 0 },
																{ 22548, -250136, 216505, -12248, 0 },
																{ 22544, -251106, 216546, -12248, 0 },
																{ 22544, -251596, 216578, -12248, 0 },
																{ 22548, -249961, 216697, -12248, 0 },
																{ 22547, -251631, 216690, -12248, 0 },
																{ 22542, -251771, 216674, -12248, 0 },
																{ 22544, -251456, 216482, -12248, 0 },
																{ 22543, -251701, 216514, -12248, 0 },
																{ 22546, -251529, 217852, -12328, 0 },
																{ 22538, -251476, 217718, -12296, 0 },
																{ 22537, -251422, 217874, -12328, 0 },
																{ 22546, -252266, 217005, -12288, 0 },
																{ 22546, -252215, 216823, -12256, 0 },
																{ 22538, -252215, 216823, -12256, 0 },
																{ 22537, -252106, 216942, -12280, 0 },
																{ 22546, -251378, 217733, -12301, 2743 },
																{ 22543, -251364, 216662, -12253, 2891 },
																{ 22548, -250154, 216667, -12253, 4364 },
																{ 22541, -249982, 216487, -12253, 57106 } };
	private static final int[][]	FORT_SPAWNS_UPPER			=
																{
																{ 22549, -252747, 213732, -11552, 0 },
																{ 22549, -252779, 213627, -11552, 0 },
																{ 22549, -252781, 213527, -11552, 0 },
																{ 22549, -252736, 213452, -11552, 0 },
																{ 22549, -252665, 213390, -11552, 0 },
																{ 22549, -252337, 213645, -11552, 0 },
																{ 22549, -252342, 213712, -11552, 0 },
																{ 22549, -252383, 213776, -11552, 0 },
																{ 22549, -252443, 213819, -11552, 0 },
																{ 22549, -250462, 213441, -11552, 0 },
																{ 22549, -250269, 213387, -11552, 0 },
																{ 22549, -250378, 213389, -11552, 0 },
																{ 22549, -252480, 213364, -11552, 49152 },
																{ 22549, -252407, 213415, -11552, 49152 },
																{ 22549, -252362, 213478, -11552, 49152 },
																{ 22549, -252569, 213372, -11552, 49152 },
																{ 22549, -252603, 213852, -11552, 16384 },
																{ 22549, -252674, 213818, -11552, 16384 },
																{ 22549, -252339, 213566, -11552, 16384 },
																{ 18778, -252556, 213600, -11544, 16384 },
																{ 22549, -252016, 214446, -11704, 16384 },
																{ 22549, -252096, 214446, -11704, 16384 },
																{ 22549, -252160, 214446, -11704, 16384 },
																{ 22549, -252288, 214446, -11704, 16384 },
																{ 22549, -252224, 214446, -11704, 16384 },
																{ 22549, -250172, 213429, -11552, 16384 },
																{ 18778, -250324, 213588, -11544, 16384 },
																{ 22593, -251326, 214460, -11640, 16384 },
																{ 22549, -250097, 213617, -11552, 16384 },
																{ 22549, -252514, 213830, -11552, 16384 },
																{ 22549, -250110, 213513, -11552, 16384 },
																{ 22549, -250534, 213627, -11552, 32767 },
																{ 22549, -250151, 213713, -11552, 16384 },
																{ 22549, -250543, 213547, -11552, 32767 },
																{ 22549, -250515, 213689, -11552, 32767 },
																{ 22549, -250432, 213808, -11552, 16384 },
																{ 22549, -250485, 213745, -11552, 32767 },
																{ 22549, -250368, 213824, -11552, 16384 },
																{ 22549, -250222, 213775, -11552, 16384 },
																{ 22549, -250288, 213824, -11552, 16384 },
																{ 22593, -251550, 214460, -11640, 16384 },
																{ 22593, -251730, 214460, -11640, 16384 },
																{ 22549, -250644, 214446, -11704, 16384 },
																{ 22549, -250832, 214446, -11704, 16384 },
																{ 22549, -250704, 214446, -11704, 16384 },
																{ 22549, -250768, 214446, -11704, 16384 },
																{ 22549, -250896, 214446, -11704, 16384 },
																{ 22537, -251434, 214460, -11640, 16384 },
																{ 22593, -251157, 214564, -11640, 16384 },
																{ 22593, -251154, 214222, -11640, 16384 },
																{ 22593, -251720, 214564, -11640, 16384 },
																{ 22593, -251718, 214222, -11640, 16384 },
																{ 22593, -251722, 214290, -11640, 16384 },
																{ 22593, -251633, 214290, -11640, 16384 },
																{ 22593, -251540, 214290, -11640, 16384 },
																{ 22593, -251331, 214290, -11640, 16384 },
																{ 22593, -251147, 214290, -11640, 16384 },
																{ 22593, -251237, 214290, -11640, 16384 },
																{ 22593, -251146, 214460, -11640, 16384 },
																{ 22593, -251643, 214460, -11640, 16384 },
																{ 22593, -251237, 214460, -11640, 16384 },
																{ 22549, -252407, 213415, -11552, 3865 },
																{ 22549, -252480, 213364, -11552, 49152 } };
	private static final int[][]	FORT_SPAWNS_GROUND			=
																{
																{ 22596, -251494, 214912, -12088, 16384 },
																{ 22596, -251280, 214911, -12088, 16384 },
																{ 22596, -251565, 214912, -12088, 16384 },
																{ 22549, -251921, 213570, -12080, 16384 },
																{ 22597, -251929, 213686, -12080, 16384 },
																{ 22536, -251264, 214224, -12080, 16384 },
																{ 22596, -250803, 213178, -12056, 32767 },
																{ 22547, -250732, 213177, -12024, 32767 },
																{ 22596, -250797, 213103, -12048, 32767 },
																{ 22596, -250799, 213260, -12048, 32767 },
																{ 22549, -251114, 213570, -12080, 16384 },
																{ 22547, -251522, 213558, -12080, 16384 },
																{ 22593, -251424, 213565, -12080, 16384 },
																{ 22547, -251332, 213563, -12080, 16384 },
																{ 22593, -251264, 213566, -12080, 16384 },
																{ 22593, -251183, 213564, -12080, 16384 },
																{ 22549, -251781, 213572, -12080, 16384 },
																{ 22593, -251696, 213566, -12080, 16384 },
																{ 22593, -251613, 213561, -12080, 16384 },
																{ 22597, -251114, 213686, -12080, 16384 },
																{ 22549, -251854, 213572, -12080, 16384 },
																{ 22549, -251039, 213570, -12080, 16384 },
																{ 22549, -250960, 213573, -12080, 16384 },
																{ 22597, -250966, 213689, -12080, 16384 },
																{ 22597, -251040, 213689, -12080, 16384 },
																{ 22597, -251261, 213681, -12080, 16384 },
																{ 22597, -251183, 213684, -12080, 16384 },
																{ 22597, -251853, 213683, -12080, 16384 },
																{ 22597, -251774, 213683, -12080, 16384 },
																{ 22597, -251693, 213683, -12080, 16384 },
																{ 22597, -251606, 213684, -12080, 16384 },
																{ 22596, -251351, 214912, -12088, 16384 },
																{ 22596, -251338, 213683, -12080, 16384 },
																{ 22596, -251522, 213683, -12080, 16384 },
																{ 22596, -251430, 213684, -12080, 16384 },
																{ 22537, -251431, 213567, -12080, 16384 },
																{ 22537, -251289, 213567, -12080, 16384 },
																{ 22536, -251373, 214220, -12080, 16384 },
																{ 22536, -251482, 214219, -12080, 16384 },
																{ 22536, -251600, 214224, -12080, 16384 },
																{ 22537, -251566, 213565, -12088, 15826 },
																{ 22593, -251495, 214792, -12080, 16384 },
																{ 22593, -251281, 214786, -12080, 16384 },
																{ 22593, -251352, 214787, -12080, 16384 },
																{ 22593, -251568, 214789, -12080, 16384 },
																{ 22596, -251441, 214672, -12080, 16384 },
																{ 22547, -251427, 214790, -12080, 16384 },
																{ 22596, -251281, 214671, -12080, 16384 },
																{ 22596, -251361, 214671, -12080, 16384 },
																{ 22596, -251505, 214672, -12080, 16384 },
																{ 22596, -251569, 214672, -12080, 16384 },
																{ 22596, -251424, 214912, -12088, 16384 },
																{ 22537, -251262, 214226, -12080, 18077 },
																{ 22596, -252094, 213194, -12048, 0 },
																{ 22547, -252210, 213189, -11992, 0 },
																{ 22596, -252096, 213120, -12048, 0 },
																{ 22596, -252089, 213271, -12048, 0 } };
	private static final int[][]	FORT_PORTALS				=
																{
																{ 18696, -249924, 218225, -12334, 16384 },
																{ 18696, -252938, 218225, -12334, 16384 } };
	private static final int[][]	THRONE_SPAWNS				=
																{
																{ TIAT, -250403, 207273, -11952, 16384 },
																{ 22536, -249848, 207587, -11960, 16384 },
																{ 22542, -249927, 207968, -11968, 16384 },
																{ 22542, -250094, 207968, -11968, 16384 },
																{ 22542, -250687, 207968, -11968, 16384 },
																{ 22542, -250839, 207968, -11968, 16384 },
																{ 22543, -251004, 207968, -11968, 16384 },
																{ 22538, -249927, 208133, -11968, 16384 },
																{ 22538, -250094, 208133, -11968, 16384 },
																{ 22538, -250687, 208133, -11968, 16384 },
																{ 22538, -250839, 208133, -11968, 16384 },
																{ 22539, -251004, 208133, -11968, 16384 },
																{ 22539, -251168, 208133, -11968, 16384 },
																{ 22539, -251332, 208133, -11968, 16384 },
																{ 22539, -251496, 208133, -11968, 16384 },
																{ 22542, -249927, 208286, -11960, 16384 },
																{ 22542, -250094, 208293, -11968, 16384 },
																{ 22542, -250687, 208287, -11968, 16384 },
																{ 22542, -250839, 208287, -11960, 16384 },
																{ 22543, -251332, 208287, -11960, 16384 },
																{ 22543, -251004, 208287, -11960, 16384 },
																{ 22543, -251496, 208287, -11968, 16384 },
																{ 22543, -251168, 208287, -11960, 16384 },
																{ 22537, -250071, 207344, -11960, 16384 },
																{ 22537, -250688, 207344, -11968, 16384 },
																{ 22537, -251086, 207344, -11960, 16384 },
																{ 22536, -250071, 207587, -11968, 16384 },
																{ 22536, -251086, 207587, -11952, 16384 },
																{ 22536, -250688, 207587, -11968, 16384 },
																{ 22536, -250881, 207587, -11960, 16384 },
																{ 22543, -249763, 207968, -11968, 16384 },
																{ 22539, -249601, 208133, -11968, 16384 },
																{ 22539, -249273, 208133, -11968, 16384 },
																{ 22539, -249437, 208133, -11968, 16384 },
																{ 22539, -249763, 208133, -11968, 16384 },
																{ 22543, -249437, 208286, -11960, 16384 },
																{ 22543, -249273, 208284, -11968, 16384 },
																{ 22543, -249601, 208284, -11960, 16384 },
																{ 22543, -249763, 208286, -11960, 16384 },
																{ 22537, -249848, 207344, -11952, 16384 },
																{ 22536, -249621, 207587, -11952, 16384 } };

	// Initialization at 6:30 am on Wednesday and Saturday
	private static final int		RESET_HOUR					= 6;
	private static final int		RESET_MIN					= 30;
	private static final int		RESET_DAY_1					= 4;
	private static final int		RESET_DAY_2					= 7;

	private int						_instanceId					= 0;

	public SeedOfDestruction(int questId, String name, String descr, String folder)
	{
		super(questId, name, descr, folder);

		addStartNpc(ALENOS);
		addTalkId(ALENOS);
		addStartNpc(TELEPORT);
		addTalkId(TELEPORT);
		addAttackId(OBELISK);
		addSpawnId(OBELISK);
		addKillId(OBELISK);
		addSpawnId(POWERFUL_DEVICE);
		addKillId(POWERFUL_DEVICE);
		addSpawnId(THRONE_POWERFUL_DEVICE);
		addKillId(THRONE_POWERFUL_DEVICE);
		addAttackId(TIAT);
		addKillId(TIAT);
		addAggroRangeEnterId(TIAT_VIDEO_NPC[0]);
		for (int mobId : MOB_IDS)
			addKillId(mobId);
	}

	@Override
	protected final boolean canEnter(L2Player player)
	{
		if (player.getParty() == null)
		{
			player.sendPacket(new SystemMessage(2101));
			return false;
		}
		L2CommandChannel channel = player.getParty().getCommandChannel();
		if (channel == null)
		{
			player.sendPacket(new SystemMessage(2103));
			return false;
		}
		else if (channel.getChannelLeader() != player)
		{
			player.sendPacket(new SystemMessage(2185));
			return false;
		}
		else if (channel.getMemberCount() < MIN_PLAYERS || channel.getMemberCount() > MAX_PLAYERS)
		{
			player.sendPacket(new SystemMessage(2102));
			return false;
		}
		for (L2Player channelMember : channel.getMembers())
		{
			if (channelMember.getLevel() < 75)
			{
				SystemMessage sm = new SystemMessage(2097);
				sm.addPcName(channelMember);
				channel.broadcastToChannelMembers(sm);
				return false;
			}
			if (!Util.checkIfInRange(1000, player, channelMember, true))
			{
				SystemMessage sm = new SystemMessage(2096);
				sm.addPcName(channelMember);
				channel.broadcastToChannelMembers(sm);
				return false;
			}
			Long reentertime = InstanceManager.getInstance().getInstanceTime(channelMember.getObjectId(), INSTANCE_ID);
			if (System.currentTimeMillis() < reentertime)
			{
				SystemMessage sm = new SystemMessage(2100);
				sm.addPcName(channelMember);
				channel.broadcastToChannelMembers(sm);
				return false;
			}
		}
		return true;
	}

	@Override
	protected final void enterInstance(L2Player player)
	{
		final int[] coords =
		{ -242759, 219981, -9986 };

		InstanceWorld world = InstanceManager.getInstance().getPlayerWorld(player);
		if (world != null)
		{
			if (!(world instanceof SODWorld))
			{
				player.sendPacket(new SystemMessage(SystemMessageId.ALREADY_ENTERED_ANOTHER_INSTANCE_CANT_ENTER));
				return;
			}
			teleportPlayer(player, coords, _instanceId);
		}
		else
		{
			if (!canEnter(player))
				return;
			_instanceId = InstanceManager.getInstance().createDynamicInstance(TEMPLATE);
			world = new SODWorld();
			world.instanceId = _instanceId;
			world.status = 0;
			InstanceManager.getInstance().addWorld(world);
			spawnState((SODWorld) world);

			for (L2DoorInstance door : InstanceManager.getInstance().getInstance(_instanceId).getDoors())
				if (ArrayUtils.contains(ATTACKABLE_DOORS, door.getDoorId()))
					door.setIsSeedOfDestructionAttackableDoor(true);

			final L2Party party = player.getParty();
			if (party == null || party.getPartyMembers() == null)
				world.allowed.add(player.getObjectId());
			else
				for (L2Player channelMember : party.getPartyMembers())
					world.allowed.add(channelMember.getObjectId());

			teleportParty(player, coords, _instanceId);

			_log.info(QN + " : created " + TEMPLATE + " Instance: " + _instanceId + " by player: " + player.getName());
		}
	}

	@Override
	protected final void exitInstance(L2Player player)
	{
		final int[] coords =
		{ -248717, 250260, 4337 };
		teleportPlayer(player, coords, 0);
	}

	private final boolean checkKillProgress(L2Npc mob, SODWorld world)
	{
		if (world.npcList.containsKey(mob))
			world.npcList.put(mob, true);
		for (boolean isDead : world.npcList.values())
			if (!isDead)
				return false;
		return true;
	}

	private final void spawnState(SODWorld world)
	{
		world.npcList.clear();
		switch (world.status)
		{
			case 0:
				spawn(world, ENTRANCE_GROUND_SPAWNS_1, true, false);
				spawn(world, ENTRANCE_GROUND_SPAWNS_2, false, false);
				spawn(world, ENTRANCE_UPPER_SPAWNS, false, true);
				break;
			case 1:
				ExShowScreenMessage message1 = new ExShowScreenMessage(1, 0, 5, 0, 1, 0, 0, false, 10000, 1,
						"The enemies have attacked. Everyone come out and fight!!!! ... Urgh~!");
				sendScreenMessage(world, message1);
				for (int i : ENTRANCE_ROOM_DOORS)
					openDoor(i, world.instanceId);
				spawn(world, SQUARE_SPAWNS_STATIC, false, true);
				spawn(world, SQUARE_SPAWNS_MAIN, true, false);
				break;
			case 2:
			case 3:
				// handled elsewhere
				return;
			case 4:
				ExShowScreenMessage message2 = new ExShowScreenMessage(1, 0, 5, 0, 1, 0, 0, false, 10000, 1,
						"Obelisk has collapsed. Don't let the enemies jump around wildly anymore!!!!");
				sendScreenMessage(world, message2);
				for (int i : SQUARE_DOORS)
					openDoor(i, world.instanceId);
				spawn(world, CORRIDOR_SPAWNS_UPPER, false, true);
				spawn(world, CORRIDOR_SPAWNS_GROUND, false, false);
				world.killedDevice = 0;
				break;
			case 5:
				openDoor(SCOUTPASS_DOOR, world.instanceId);
				spawn(world, SQUARE_SPAWNS_HALF, false, false);
				spawn(world, SCOUTPASS_SPAWNS_UPPER, false, true);
				spawn(world, SCOUTPASS_SPAWNS_GROUND, false, false);
				spawn(world, PREFORT_SPAWNS, false, false);
				spawn(world, FORT_SPAWNS_UPPER, false, true);
				spawn(world, FORT_SPAWNS_GROUND, false, false);
				L2Npc npc = addSpawn(TIAT_VIDEO_NPC[0], TIAT_VIDEO_NPC[1], TIAT_VIDEO_NPC[2], TIAT_VIDEO_NPC[3], 0, false, 0, false, world.instanceId);
				npc.setIsNoRndWalk(true);
				startQuestTimer("DoorCheck", 10000, npc, null);
				world.killedDevice = 0;
				break;
			case 6:
				openDoor(THRONE_DOOR, world.instanceId);
				spawn(world, THRONE_SPAWNS, false, false);
				break;
			case 7:
				// handled elsewhere
				return;
			case 8:
				ExShowScreenMessage message3 = new ExShowScreenMessage(1, 0, 5, 0, 1, 0, 0, false, 10000, 1, "Come out, warriors. Protect Seed of Destruction");
				sendScreenMessage(world, message3);
				break;
			case 9:
				// instance end
				break;
		}
		world.status++;
	}

	private final void spawn(SODWorld world, int[][] spawnTable, boolean addToKillTable, boolean isImmobilized)
	{
		for (int[] mob : spawnTable)
		{
			L2Npc npc = addSpawn(mob[0], mob[1], mob[2], mob[3], mob[4], false, 0, false, world.instanceId);
			if (addToKillTable)
				world.npcList.put(npc, false);
			npc.setIsImmobilized(TIAT == mob[0] ? true : isImmobilized);
			npc.setIsNoRndWalk(true);
		}
	}

	private final void setInstanceTimeRestrictions(SODWorld world)
	{
		Calendar reenter = Calendar.getInstance();
		reenter.set(Calendar.MINUTE, RESET_MIN);
		reenter.set(Calendar.HOUR_OF_DAY, RESET_HOUR);
		// if time is >= RESET_HOUR - roll to the next day
		if (reenter.getTimeInMillis() <= System.currentTimeMillis())
			reenter.add(Calendar.DAY_OF_MONTH, 1);
		if (reenter.get(Calendar.DAY_OF_WEEK) <= RESET_DAY_1)
			while (reenter.get(Calendar.DAY_OF_WEEK) != RESET_DAY_1)
				reenter.add(Calendar.DAY_OF_MONTH, 1);
		else
			while (reenter.get(Calendar.DAY_OF_WEEK) != RESET_DAY_2)
				reenter.add(Calendar.DAY_OF_MONTH, 1);

		SystemMessage sm = new SystemMessage(SystemMessageId.INSTANT_ZONE_S1_ENTRY_RESTRICTED);
		sm.addString(InstanceManager.getInstance().getInstanceIdName(INSTANCE_ID));

		// set instance reenter time for all allowed players
		for (int objectId : world.allowed)
		{
			L2Player player = L2World.getInstance().getPlayer(objectId);
			InstanceManager.getInstance().setInstanceTime(objectId, INSTANCE_ID, reenter.getTimeInMillis());
			if (player != null && player.isOnline() > 0)
				player.sendPacket(sm);
		}
	}

	private final void sendScreenMessage(SODWorld world, ExShowScreenMessage message)
	{
		for (int objId : world.allowed)
		{
			L2Player player = L2World.getInstance().getPlayer(objId);
			if (player != null)
				player.sendPacket(message);
		}
	}

	@Override
	public final String onAggroRangeEnter(L2Npc npc, L2Player player, boolean isPet)
	{
		if (npc.getNpcId() == 29167)
		{
			if (isPet == false && player != null)
			{
				InstanceWorld tmpworld = InstanceManager.getInstance().getWorld(player.getInstanceId());
				if (tmpworld instanceof SODWorld)
				{
					SODWorld world = (SODWorld) tmpworld;
					if (world.status == 7)
					{
						world.status++;
						for (int objId : world.allowed)
						{
							L2Player pl = L2World.getInstance().getPlayer(objId);
							if (pl != null)
								pl.showQuestMovie(5);
						}
					}
				}
				npc.deleteMe();
			}
		}
		return null;
	}

	@Override
	public final String onAttack(L2Npc npc, L2Player attacker, int damage, boolean isPet, L2Skill skill)
	{
		InstanceWorld tmpworld = InstanceManager.getInstance().getWorld(npc.getInstanceId());
		if (tmpworld instanceof SODWorld)
		{
			SODWorld world = (SODWorld) tmpworld;
			if (world.status == 2 && npc.getNpcId() == OBELISK)
			{
				world.status = 4;
				spawn(world, SQUARE_SPAWNS_MAIN, false, false);
			}
			else if (world.status == 3 && npc.getNpcId() == OBELISK)
			{
				world.status = 4;
				spawn(world, SQUARE_SPAWNS_HALF, false, false);
			}
			else if (world.status <= 8 && npc.getNpcId() == TIAT)
			{
				if (npc.getCurrentHp() < (npc.getMaxHp() / 2))
				{
					spawnState(world);
					setInstanceTimeRestrictions(world);
				}
			}
		}
		return null;
	}

	@Override
	public final String onAdvEvent(String event, L2Npc npc, L2Player player)
	{
		InstanceWorld tmpworld = InstanceManager.getInstance().getWorld(npc.getInstanceId());
		if (tmpworld instanceof SODWorld)
		{
			SODWorld world = (SODWorld) tmpworld;
			if (event.equalsIgnoreCase("TeleportOut"))
			{
				exitInstance(player);
			}
			else if (event.equalsIgnoreCase("DoorCheck"))
			{
				L2DoorInstance tmp = InstanceManager.getInstance().getInstance(npc.getInstanceId()).getDoor(FORTRESS_DOOR);
				if (tmp.getCurrentHp() < tmp.getMaxHp())
				{
					world.deviceSpawnedMobCount = 0;
					spawn(world, FORT_PORTALS, false, true);
				}
				else
					startQuestTimer("DoorCheck", 10000, npc, null);
			}
		}
		return "";
	}

	@Override
	public final String onKill(L2Npc npc, L2Player player, boolean isPet)
	{
		InstanceWorld tmpworld = InstanceManager.getInstance().getWorld(npc.getInstanceId());
		if (tmpworld instanceof SODWorld)
		{
			SODWorld world = (SODWorld) tmpworld;
			if (world.status == 1)
			{
				if (checkKillProgress(npc, world))
					spawnState(world);
			}
			else if (world.status == 2)
			{
				if (checkKillProgress(npc, world))
					world.status++;
			}
			else if (world.status == 4 && npc.getNpcId() == OBELISK)
			{
				spawnState(world);
			}
			else if (world.status == 5 && npc.getNpcId() == POWERFUL_DEVICE)
			{
				world.killedDevice++;
				if (world.killedDevice >= 3)
					spawnState(world);
			}
			else if (world.status == 6 && npc.getNpcId() == THRONE_POWERFUL_DEVICE)
			{
				world.killedDevice++;
				if (world.killedDevice >= 2)
					spawnState(world);
			}
			else if (world.status >= 7)
			{
				if (npc.getNpcId() == TIAT)
				{
					world.status++;
					for (int objId : world.allowed)
					{
						L2Player pl = L2World.getInstance().getPlayer(objId);
						if (pl != null)
							pl.showQuestMovie(6);
					}
					SeedOfDestructionManager.getInstance().increaseTiatKills();
					for (L2Npc mob : InstanceManager.getInstance().getInstance(world.instanceId).getNpcs())
						mob.deleteMe();
				}
			}
		}
		return "";
	}

	@Override
	public final String onTalk(L2Npc npc, L2Player player)
	{
		final int npcId = npc.getNpcId();
		final byte state = SeedOfDestructionManager.getInstance().getState();

		QuestState st = player.getQuestState(QN);
		if (st == null)
			st = newQuestState(player);

		if (npcId == ALENOS)
		{
			if (state == SeedOfDestructionManager.STATE_ATTACK)
				enterInstance(player);
			else if (state == SeedOfDestructionManager.STATE_DEFENSE)
			{
				final int[] coords =
				{ -245800, 220488, -12112 };
				teleportPlayer(player, coords, 0);
			}
		}
		else if (npcId == TELEPORT)
		{
			final int[] coords =
			{ -245802, 220528, -12104 };
			teleportPlayer(player, coords, _instanceId);
		}

		return "";
	}

	public static void main(String[] args)
	{
		new SeedOfDestruction(-1, QN, "Seed of Destruction", "instances");
	}
}
